package org.record.mood.web.intercept;

import lombok.extern.slf4j.Slf4j;
import org.record.mood.common.RecordConst;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:登录拦截器
 * @author: sunyw
 * @time: 2020-04-15 15:59
 */
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        Object obj = request.getSession().getAttribute(RecordConst.USER_LOGIN_TOCKEN);
        if (null != obj) {
            return true;
        }
        response.sendRedirect("/admin/index");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }

    public static void main(String[] args) {
        String s ="/admin/register.html";
        System.out.println(s.substring(7));
    }
}
